package xzy.com.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xzy.com.dao.UserMapper;
import xzy.com.entity.domain.Users;
import xzy.com.service.UserService;

//1.继承ServiceImpl,传入<UserMapper,User>类作范型参数
//2.实现自定义的Service接口
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, Users> implements UserService {
	/*
	 * @Autowired private UserMapper userMapper;
	 */

	@Override
	public Users getById(Long id) {
		return this.baseMapper.findUserById(id);
	}

	@Override
	public Users getByName(String name) {
		QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("account", name);
		List<Users> list = this.baseMapper.selectList(queryWrapper);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Map<String, Object> listPages(int pages, int size) {
		// System.out.println("分页："+pages+":"+size);
		// 定义查询器
		QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
		queryWrapper.orderByDesc("id");
		// 设置分页,false参数标志不统计数据
		Page<Users> page = new Page<>(pages, size);
		// 分页查询

		IPage<Users> ipage = this.baseMapper.selectPage(page, null);
		Map<String, Object> map = new HashMap<>();
		// 获取列表记录
		map.put("list", ipage.getRecords());
		map.put("pageTotal", ipage.getPages());
		map.put("recordTotal", this.baseMapper.selectCount(null));
		return map;
	}

	@Override
	public Map<String, Object> searchListPages(String name, String address, int pages, int size) {
		System.out.println("分页：" + name + ":" + address);
		// 定义查询器
		QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
		// 设置搜索条件
		if ((name == null || name.isEmpty()) && (address != null || !address.isEmpty())) {
			queryWrapper.like("addr", address);
		}
		if ((name != null || !name.isEmpty()) && (address == null || address.isEmpty())) {
			queryWrapper.like("user_name", name);
		}
		if ((name != null && !name.isEmpty()) && (address != null && !address.isEmpty())) {
			queryWrapper.like("user_name", name).like("addr", address);
		}
		if ((name == null || name.isEmpty()) && (address == null || address.isEmpty())) {
			queryWrapper = null;
		}
		// 设置分页,false参数标志不统计数据
		Page<Users> page = new Page<>(pages, size);
		// 分页查询
		IPage<Users> ipage = this.baseMapper.selectPage(page, queryWrapper);
		Map<String, Object> map = new HashMap<>();
		// 获取列表记录
		map.put("list", ipage.getRecords());
		map.put("pageTotal", ipage.getPages());
		map.put("recordTotal", this.baseMapper.selectCount(queryWrapper));
		return map;
	}

}
